/**
 * Created by apple on 16/11/14.
 */
window.onload = function () {
    if (storage_admin_id) {
        user_list();
        $('#dataTables-example').dataTable();
        $("#sideNav").click(function () {
            if ($(this).hasClass('closed')) {
                $('.navbar-side').animate({left: '0px'});
                $(this).removeClass('closed');
                $('#page-wrapper').animate({'margin-left': '260px'});

            }
            else {
                $(this).addClass('closed');
                $('.navbar-side').animate({left: '-260px'});
                $('#page-wrapper').animate({'margin-left': '0px'});
            }
        });
        $('.loading').hide();
    } else {
        window.location.href = "login.html"
    }

};
function user_list() {
    $.ajax({
        type: 'GET',
        url: "http://118.178.59.93:8080/Motocycle/api/v1/users",
        async: false,
        success: function (data) {
            var obj = JSON.parse(data);
            try {
                if (obj.code == 0) {
                    var user_list_total = "";
                    $.each(obj.data.rows, function (i, item) {
                        user_list_total += "<tr>" +
                            "<td><a href='user_details.html?user_id=" + item.user_id + "' target='_blank'>" + item.nickname + "</a></td>";
                        if (item.avatar) {
                            user_list_total += "<td><img src=" + item.avatar + " style='width: 20px;height: 20px;'></td>"

                        } else {
                            user_list_total += "<td><img src='assets/img/none.png' style='width: 20px;height: 20px;'></td>"
                        }
                        if (item.is_admin == 1) {
                            user_list_total += "<td>是</td>"
                        } else {
                            user_list_total += "<td></td>"
                        }
                        user_list_total += "<td>" + item.level + "</td>" +
                            "<td>" + item.integral + "</td>" +
                            "<td>" + item.gold + "</td>" +
                            "<td>" + item.createdDate.substring(0, 11) + "</td>" +
                            "<td>" + item.login_day + "</td>" +
                            "<td>" + item.login_date.substring(0, 11) + "</td>";
                        if (item.is_forbidden == 1) {
                            user_list_total += "<td class='user_edit' data-id=" + item.user_id + "><span style='padding-right: 15px;'>是</span><i class='fa fa-pencil' aria-hidden='true'></i></td>"
                        } else {
                            user_list_total += "<td class='user_edit' data-id=" + item.user_id + "><span style='color: transparent;padding-right: 15px;'>否</span><i class='fa fa-pencil' aria-hidden='true'></i></td>"
                        }
                        user_list_total += "</tr>"
                    });
                    $('#dataTables-example tbody').html(user_list_total);

                }
            } catch (e) {

            }
        }
    })

}

$(document).on('click', '.user_edit', function () {
    var data_id = $(this).attr('data-id');
    var _html = $(this).children('span').html();
    if (_html == "是") {
        layer.msg('真的要解封该用户吗？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: 'PUT',
                    url: "http://118.178.59.93:8080/Motocycle/api/v1/users/forbidden",
                    data: {user_id: data_id},
                    async: false,
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            layer.msg('解封成功!', {time: 3000});
                            user_list();
                        }
                    }
                })
            }
        });
    } else {
        layer.msg('真的要封禁该用户吗？', {
            time: 0, //不自动关闭
            btn: ['确定', '取消'],
            yes: function (index) {
                layer.close(index);
                $.ajax({
                    type: 'PUT',
                    url: "http://118.178.59.93:8080/Motocycle/api/v1/users/forbidden",
                    data: {user_id: data_id},
                    async: false,
                    success: function (data) {
                        var obj = JSON.parse(data);
                        if (obj.code == 0) {
                            layer.msg('封禁成功!', {time: 3000});
                            user_list();
                        }
                    }
                })
            }
        });
    }
});